package com.example.qmjy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.example.qmjy.R;
import com.example.qmjy.adapter.MyPagerAdapter;
import com.example.qmjy.constant.Constant;
import com.example.qmjy.util.AppSpUtils;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;

public class WelcomeActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {
    private static final String TAG = "WelcomeActivity";
    private ArrayList<View> guidePages;
    private ViewPager guideViewPager;
    private SimpleDraweeView backgroundImage;
    private ImageView enterButton;
    private boolean isFirstRun;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        isFirstRun = AppSpUtils.getValueFromPreferences(this, Constant.SP_IsFirstRun, true);
        initViews();
        initEvent();
    }

    private void initViews() {
        guideViewPager = findViewById(R.id.guide_vp);
        backgroundImage = findViewById(R.id.guide_sdv);
        enterButton = findViewById(R.id.experience_iv);

        if (isFirstRun) {
            // 首次运行：显示引导页
            AppSpUtils.putValueToPreferences(this, Constant.SP_IsFirstRun, false);
            setupGuidePages();
        } else {
            // 非首次运行：直接跳转主页面（延迟 2 秒）
            jumpToMainActivityWithDelay();
        }
    }

    // 初始化事件
    public void initEvent() {
        guideViewPager.addOnPageChangeListener(this);
        guideViewPager.setOnClickListener(this);
    }

    // 引导页集合
    private void setupGuidePages() {
        backgroundImage.setVisibility(View.GONE);
        guideViewPager.setVisibility(View.VISIBLE);

        // 初始化引导页
        guidePages = new ArrayList<>();
        LayoutInflater inflater = getLayoutInflater();
        guidePages.add(inflater.inflate(R.layout.view_one, null, false));
        guidePages.add(inflater.inflate(R.layout.view_two, null, false));
        guidePages.add(inflater.inflate(R.layout.view_three, null, false));
        guidePages.add(inflater.inflate(R.layout.view_four, null, false));

        // 设置 ViewPager 适配器和监听
        guideViewPager.setAdapter(new MyPagerAdapter(guidePages, this));
        guideViewPager.addOnPageChangeListener(this);
        enterButton.setOnClickListener(this);
    }

    // 延迟跳转到首页
    private void jumpToMainActivityWithDelay() {
        backgroundImage.setVisibility(View.VISIBLE);
        backgroundImage.setImageResource(R.mipmap.welcome);

        // 使用 View.postDelayed 替代 Handler，避免内存泄漏
        getWindow().getDecorView().postDelayed(() -> {
            if (!isFinishing()) {
                startActivity(new Intent(this, MainActivity.class));
                finish();
            }
        }, 2000);
    }

    // 点击“立即体验”按钮
    @Override
    public void onClick(View v) {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    // ViewPager 滑动监听
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        // 滑动到最后一页时显示按钮
        if (position == 3) {
            enterButton.setVisibility(View.VISIBLE);
        } else {
            enterButton.setVisibility(View.GONE);
        }
    }

    @Override
    public void onPageSelected(int position) {
        // 可在此处处理页面选中逻辑（如果需要）
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        // 可在此处处理滑动状态变化（如果需要）
    }

    @Override
    protected void onDestroy() {
        // 清除所有回调（保险操作）
        getWindow().getDecorView().removeCallbacks(null);
        super.onDestroy();
    }
}